﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TheDrinksApp.Database;
using TheDrinksApp.Screen.CurrentOrder.TableViewCell;
using TheDrinksApp.Models;
using System.Collections.ObjectModel;
using SumaryTableViewCell = TheDrinksApp.Screen.CurrentOrder.TableViewCell.SumaryTableViewCell;

namespace TheDrinksApp.Screen.CurrentOrder
{
    public partial class SumaryScreen : UserControl
    {
        public MainPage parent;
        private float howMuch;
        public float HowMuch
        {
            get { return howMuch; }
            set
            {
                howMuch = value;
            }
        }

        public SumaryScreen(MainPage _parent)
        {
            InitializeComponent();
            parent = _parent;
            howMuch = 0;
        }

        private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
        {
            if (Util.goFromRecentOrCurrent == 0)
            {
                imageAddDrink.Visibility = Visibility.Visible;
                imageFinish.Visibility = Visibility.Visible; ;
                gridListItem.Height = 404;
                lbResult.Height = 404;
                howMuch = Util.HowMuch;
                if (howMuch >= 0)
                {
                    tbHow.Text = "TOTAL :";
                    tbMuch.Text = Util.GetCurrencySymbol() + Math.Round(howMuch, 2).ToString("0.00");
                }
                else
                {
                    tbHow.Text = "HOW";
                    tbMuch.Text = "MUCH ?";
                }
                lbResult.Items.Clear();
                for (int i = 0; i < Util.currentOrders.Count; i++)
                {
                    lbResult.Items.Add(new SumaryTableViewCell(Util.currentOrders[i], this, i));
                }
            }
            else
            {
                imageAddDrink.Visibility = Visibility.Collapsed;
                imageFinish.Visibility = Visibility.Collapsed;
                gridListItem.Height = 504;
                lbResult.Height = 504;
                lbResult.Items.Clear();
                for (int i = 0; i < Util.recentOrders.Count; i++)
                {
                    SumaryTableViewCell cell = new SumaryTableViewCell(Util.recentOrders[i], this, i);
                    cell.order = Util.recentOrders[i];
                    cell.stt = i;
                    lbResult.Items.Add(cell);
                }
                if (Util.orderEditting != null)
                {
                    tbHow.Text = "TOTAL :";
                    if (Util.orderEditting.HowMuch < 0)
                    {
                        tbMuch.Text = Util.GetCurrencySymbol() + "0.00";
                    }
                    else
                    {
                        tbMuch.Text = Util.GetCurrencySymbol() + Math.Round(Util.orderEditting.HowMuch, 2).ToString("0.00");
                    }
                }
            }
        }
        public void GoToDetail(CurrentOrderDAO order)
        {
            if (Util.goFromRecentOrCurrent != 0)
                return;
            Util.nowEditting = order;
            Util.preDetailPage = PAGE.SUMARY_SCREEN;
            parent.ShowPage(PAGE.ORDER_ITEM_DETAIL);

        }
        public void DeleteAt(int index)
        {

            if (Util.goFromRecentOrCurrent == 0)
            {
                Util.currentOrders.RemoveAt(index);
                GlobalVariable.mainPage.SetCurrentNumber();
            }
            else
            {
                Util.DeleteOrderItemWithId(Util.recentOrders[index].orderItemId);
                Util.recentOrders.RemoveAt(index);
            }
            UserControl_Loaded_1(null, null);
        }

        private void Image_Tap_3(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (Util.goFromRecentOrCurrent == 0)
            {
                Util.HowMuch = howMuch;
                if (Util.currentOrders.Count == 0)
                    return;
                Util.AddToRecentOrderWithTotal(Util.HowMuch);
                parent.ShowPage(PAGE.HOME);
                GlobalVariable.mainPage.SetCurrentNumber();
            }
        }

        private void Image_Tap_4(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Util.HowMuch = -1;
            parent.ShowPage(PAGE.CHOOSE_CATEGORY);
        }

        private void Image_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
        {
            //if (Util.goFromRecentOrCurrent == 0)
            //{

            Util.type = 0;
            parent.ShowPage(PAGE.HOW_MUCH_CURRENT);
            //}
            //else
            //{
            //    Util.type = 0;
            //    parent.ShowPage(PAGE.HOW_MUCH_CURRENT);
            //}
        }

        public void hideAllDeleteButton()
        {
            foreach (var item in lbResult.Items)
            {
                ((SumaryTableViewCell)item).hideDeleteButton();
            }
        }
    }
}
